<?php

	/*
	
		Copyright: RIVS.com LLC
		Creation Date: 2013-01-17
		Update Date: 2013-01-17
		Author: Dominik Kondrat <dk(at)rivs.com>
		
		This file is example of OAuth authneticaiton for your app
	
	*/
	
	// Configuration
	$sClient = 'your client';
	$sSecret = 'your secret';
	$sUrlAuthorize = 'https://www.rivs.com/rest/authorize/?scope=all&client_id='.$sClient.'&response_type=token&state=test_state';
	$sUrlSelf = 'https://www.mydomain.com/oauth_example.php';
	
	// Handle Errors
	if(isset($_GET['error'])) {
		print_r($_GET['error_description']);
	}
	// Received Token
	else if(isset($_GET['state'])) {
		print_r('<script type="text/javascript">location.href=\''.$sUrlSelf.'?parse=\'+encodeURIComponent(location.href+\'&\'+location.hash);</script>');
	 }
	// Parse URL
	else if(isset($_GET['parse'])) {
		$aUrl = parse_url(urldecode($_GET['parse']));
		foreach(explode('&',$aUrl['fragment']) as $sV) {
			list($sName, $sVal) = explode('=',$sV);
			$aParams[$sName] = $sVal;	
		}
		print_r('Your access token is: '.$aParams['access_token']);
		die();
	 }
	 // If there is no token try to authorize.
	 else header('Location: '.$sUrlAuthorize);
	 
	die();

?>